package com.cmas.platform.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.aidex.common.core.domain.model.LoginUser;
import com.aidex.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.utils.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmas.platform.mapper.NotificationUserMapper;
import com.cmas.platform.domain.NotificationUser;
import com.cmas.platform.service.NotificationUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知公告用户阅读表Service业务层处理
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2024-05-19
 */
@Service
public class NotificationUserServiceImpl extends ServiceImpl<NotificationUserMapper, NotificationUser> implements NotificationUserService {

    private static final Logger log = LoggerFactory.getLogger(NotificationUserServiceImpl.class);


    /**
     * 查询通知公告用户阅读表列表
     *
     * @param notificationUser 通知公告用户阅读表
     * @return 通知公告用户阅读表
     */
    @Override
    public List<NotificationUser> selectNotificationUserList(NotificationUser notificationUser) {
        QueryWrapper<NotificationUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(notificationUser.getNotificationId())) {
            queryWrapper.lambda().eq(NotificationUser::getNotificationId, notificationUser.getNotificationId());
        }
        if (StringUtils.isNotEmpty(notificationUser.getUserId())) {
            queryWrapper.lambda().eq(NotificationUser::getUserId, notificationUser.getUserId());
        }
        if (StringUtils.isNotEmpty(notificationUser.getIsRead())) {
            queryWrapper.lambda().like(NotificationUser::getIsRead, notificationUser.getIsRead());
        }
        List<NotificationUser> notificationUserList = baseMapper.selectList(queryWrapper);
        return notificationUserList;
    }

    /**
     * 批量删除通知公告用户阅读表
     *
     * @param ids 需要删除的通知公告用户阅读表ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteNotificationUserByIds(String[] ids) {
        return baseMapper.deleteNotificationUserByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean readNotification(List<String> ids) {
        if (ids != null) {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            List<NotificationUser> notificationUserList = new ArrayList<>();
            for (String id : ids) {
                NotificationUser notificationUser = new NotificationUser();
                notificationUser.setNotificationId(id);
                notificationUser.setUserId(loginUser.getUserId());
                notificationUser.setIsRead("1");
                notificationUserList.add(notificationUser);

            }
            return this.saveBatch(notificationUserList);
        }
        return true;
    }

}
